package hub.cathome.service.impl;

import hub.cathome.entity.ShotBlog;
import hub.cathome.mapper.CommentMapper;
import hub.cathome.mapper.LikeMapper;
import hub.cathome.mapper.ResourceLinkMapper;
import hub.cathome.mapper.ShotBlogMapper;
import hub.cathome.service.CommentService;
import hub.cathome.service.ResourceService;
import hub.cathome.service.ShotBlogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("shotBlogService")
public class ShotBlogServiceImpl implements ShotBlogService {
    @Resource(name="shotBlogMapper")
    private ShotBlogMapper shotBlogMapper;
    @Resource(name="resourceLinkMapper")
    private ResourceLinkMapper resourceLinkMapper;
    @Resource(name="resourceService")
    private ResourceService resourceService;
    @Resource(name="commentMapper")
    private CommentMapper commentMapper;
    @Resource(name="likeMapper")
    private LikeMapper likeMapper;
    @Resource(name="commentService")
    private CommentService commentService;

    @Override
    public int insertShotBlog(ShotBlog shotBlog) {
        return shotBlogMapper.insertShotBlog(shotBlog);
    }

    @Override
    public int updateShotBlog(ShotBlog shotBlog) {
        return shotBlogMapper.updateShotBlog(shotBlog);
    }

    @Override
    public int deleteShotBlog(Integer id) {
        List<Integer> blogResourceLinkIds = resourceLinkMapper.getShotBlogResourceLinkIdByShotBlogId(id);
        for (Integer resourceLinkId : blogResourceLinkIds){
            resourceService.deleteResourceLink(resourceLinkId);
            System.out.println("[resourceService] [basic]->[delete]"+resourceLinkId);
        }
        List<Integer> commentIds = commentMapper.getCommentIdByShotBlogId(id);
        List<Integer> likeIds = likeMapper.getLikeIdByShotBlogId(id);
        for(Integer commentId:commentIds){
            commentMapper.deleteComment(commentId);
            System.out.println("[commentMapper] ->[delete]"+commentId);
        }
        for(Integer likeId:likeIds){
            likeMapper.deleteLike(likeId);
            System.out.println("[likeMapper] ->[delete]"+likeId);
        }
        return shotBlogMapper.deleteShotBlog(id);
    }

    @Override
    public ShotBlog getShotBlogById(Integer id) {
        ShotBlog shotBlog = shotBlogMapper.getShotBlogById(id);
        commentService.AddIconForComment(shotBlog.getComments());
        return shotBlog;
    }

    @Override
    public List<Integer> getShotBlogIdByUserId(Integer userId) {
        return shotBlogMapper.getShotBlogIdByUserId(userId);
    }

    @Override
    public List<ShotBlog> getShotBlogByUserId(Integer userId) {
        List<ShotBlog> shotBlogs = shotBlogMapper.getShotBlogByUserId(userId);
        for(ShotBlog shotBlog:shotBlogs){
            commentService.AddIconForComment(shotBlog.getComments());
        }
        return shotBlogs;
    }

    @Override
    public List<ShotBlog> getAllShotBlog() {
        List<ShotBlog> shotBlogs = shotBlogMapper.getAllShotBlog();;
        for(ShotBlog shotBlog:shotBlogs){
            commentService.AddIconForComment(shotBlog.getComments());
        }
        return shotBlogs;
    }

    public List<ShotBlog> getOnePage(Integer npage, Integer limit) {
        List<ShotBlog> shotBlogs = shotBlogMapper.getSome((npage-1)*limit,limit);
        for(ShotBlog shotBlog:shotBlogs){
            commentService.AddIconForComment(shotBlog.getComments());
        }
        return shotBlogs;
    }
    @Override
    public List<ShotBlog> getSome(Integer begin, Integer limit) {
        List<ShotBlog> shotBlogs = shotBlogMapper.getSome(begin,limit);
        for(ShotBlog shotBlog:shotBlogs){
            commentService.AddIconForComment(shotBlog.getComments());
        }
        return shotBlogs;
    }

    @Override
    public int getShotBlogCount() {
        return shotBlogMapper.getShotBlogCount();
    }
}
